package com.weetop.cfw.mine.activity

import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.kongzue.dialog.v3.MessageDialog
import com.tencent.mmkv.MMKV
import com.weetop.cfw.MainActivity
import com.weetop.cfw.R
import com.weetop.cfw.base.presenter.imp.SettingPresenterImp
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.base.view.SettingView
import com.weetop.cfw.bean.ErrorBean
import com.weetop.cfw.bean.SinglePageIntroduceBean
import com.weetop.cfw.constants.UrlConstants
import com.weetop.cfw.other.CommonWebViewActivity
import com.weetop.cfw.other.RichTextWebViewActivity
import com.weetop.cfw.utils.MMKVUtils
import kotlinx.android.synthetic.main.activity_setting.*


/**
 * 设置界面
 */
class SettingActivity : CommonBaseActivity(),View.OnClickListener,SettingView {

    private lateinit var settingPresenterImp: SettingPresenterImp
    private var appAboutUsStr = ""//关于我们富文本

    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.imageSettingBack -> ActivityUtils.finishActivity(this)
            R.id.relativeFeedBack -> ActivityUtils.startActivity(FeedBackActivity::class.java)
            R.id.relativeVersionInfo -> ActivityUtils.startActivity(VersionInfoActivity::class.java)
            R.id.textLogOut -> tipUserLogOut()
            R.id.relativeAboutUsContainer -> RichTextWebViewActivity.startRichTextWebViewActivity(this,"关于我们",UrlConstants.about_us_web_url,true)
        }
    }

    override fun getLayoutId() = R.layout.activity_setting

    override fun initView(savedInstanceState: Bundle?) {
       setViewsOnClickListener(this,imageSettingBack,relativeAboutUsContainer,textLogOut,relativeFeedBack,relativeVersionInfo)
    }

    override fun initData(savedInstanceState: Bundle?) {
        settingPresenterImp = SettingPresenterImp()
        settingPresenterImp.attachView(this)

        settingPresenterImp.getAboutUsData(this)
    }

    /**
     * 提醒用户是否退出登录
     */
    private fun tipUserLogOut(){
        MessageDialog.show(this,"提示","确认退出登录吗？","退出登录","取消").setOnOkButtonClickListener { baseDialog, v ->
            settingPresenterImp.logOut(this@SettingActivity)
            true
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        settingPresenterImp.detachView()
    }

    /**
     * 关于我们富文本数据获取成功
     */
    override fun aboutUsDataDetSuccess(singlePageIntroduceBean: SinglePageIntroduceBean?) {
        if(singlePageIntroduceBean != null){
            appAboutUsStr = singlePageIntroduceBean.content
        }
    }

    /**
     * 退出登录成功
     */
    override fun logOutSuccess(errorBean: ErrorBean?) {
        if(errorBean != null){
            MMKV.defaultMMKV().clearAll()
            ActivityUtils.finishAllActivities()
            ActivityUtils.startActivity(MainActivity::class.java)
        }
    }
}
